<?php
class Admin_UserController extends App_ControllerAdmin
{
    public function init()
    {
		$this->_helper->layout->setLayout('admin');
		$this->_sess = new Zend_Session_Namespace();
   		if(!@$this->_sess->session){
			$this->_helper->redirector('index','login','admin');
		}
		
	}

	function random_salt($num) {
		//Tao du lieu cho hinh ngau nhien
		$chars = array( 'a', 'A', 'b', 'B', 'c', 'C', 'd', 'D', 'e', 'E', 'f', 'F', 'g', 'G', 'h', 'H', 'i', 'I', 'j', 'J',  'k', 'K', 'l', 'L', 'm', 'M', 'n', 'N', 'o', 'p', 'P', 'q', 'Q', 'r', 'R', 's', 'S', 't', 'T',  'u', 'U', 'v', 'V', 'w', 'W', 'x', 'X', 'y', 'Y', 'z', 'Z', '1', '2', '3', '4', '5', '6', '7', '8', '9');
		$max_chars = count($chars) - 1;
		for($i = 0; $i < $num; $i++) {
		  $code = ( $i == 0 ) ? $chars[rand(0, $max_chars)] : $code . $chars[rand(0, $max_chars)];
		}
		return $code;
  	}	


	
	public function indexAction(){
		$mode = new Admin_Model_User;
		$mode = $mode->view();
		$this->view->show_user = $mode;
	}
	
	public function viewAction(){
		$mode = new Admin_Model_User;
		$mode = $mode->view();
		$this->view->show_user = $mode;
	}
	public function addAction()
	{	
		if($this->_request->isPost()){
			$Request = $this->getRequest()->getPost();
			$date = date("Y-m-d");
			$mode = new Admin_Model_User;
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['avatar'];
         	$upload->setDestination('public/images/users/');
         	$upload->receive($picture['name']);
			$image['avatar']=$picture['name'];
			$check = $mode->check($Request['email']);
			$salt =	$this->random_salt(5);
			if($check == TRUE){
					$data = array(
							'fullname'				=> $Request['fullname'],
							'email'  				=> $Request['email'],
							'password'				=> md5(md5($Request['password']).$salt),
							'address'				=> $Request['address'],
							'phone' 				=> $Request['phone'],
							'gender'				=> $Request['gender'],
							'birthday'				=> $Request['birthday'],
							'id_group'				=> $Request['group'],
							'create_date'			=> $date,
							'salt'					=> $salt,
							'avatar'				=> $image['avatar'],
					);
				$ok = $mode->add($data);
				$this->view->message = 'Thêm Dữ Liệu Thành Công !!!';	
			}
			else{
				$this->view->error = 'Tài Khoản Đã Có !!!';
			}
		}
	}
	
	public function editAction()
	{
		$mode = new Admin_Model_User;
		$id = $this->getRequest()->getParam('id');
		$data = $mode->view_where($id);
		if($this->_request->isPost()){
			$data = $mode->view_where($id);
			$Request = $this->getRequest()->getPost();
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['avatar'];
         	$upload->setDestination('public/images/users/');
         	$upload->receive($picture['name']);
			$image['avatar']=$picture['name'];
			$salt = $data['0']['salt'];
			if($Request['password']==NULL)
			{
				$password = $data['0']['password'];
			}
			else{
				$password = md5(md5($Request['password']).$salt);
			}
			
			
			if($image['avatar'] == NULL){
				$avatar = $data['0']['avatar'];
			}
			else{
				$avatar = $image['avatar'];
			}
			$data = array(
					'fullname'				=> $Request['fullname'],
					'email'  				=> $Request['email'],
					'password'				=> $password,
					'address'				=> $Request['address'],
					'phone' 				=> $Request['phone'],
					'gender'				=> $Request['gender'],
					'birthday'				=> $Request['birthday'],
					'id_group'				=> $Request['group'],
					'avatar'				=> $avatar,
					'is_delete'				=> $Request['delete'],
			);
			$mode->edit($id,$data);
			$this->view->message = 'Cập Nhật Dữ Liệu Thành Công !!!';	
		}
		$data = $mode->view_where($id);
		$this->view->edit_user = $data;
	}
	
	public function profileAction()
	{
		$mode = new Admin_Model_User;
		$id = @$this->_sess->session['id'];
		$data = $mode->view_where($id);
		
		if($this->_request->isPost()){
			$data = $mode->view_where($id);
			$Request = $this->getRequest()->getPost();
			$upload = new Zend_File_Transfer_Adapter_Http();
         	$info = $upload->getFileInfo();
         	$picture = $info['avatar'];
         	$upload->setDestination('public/images/users/');
         	$upload->receive($picture['name']);
			$image['avatar']=$picture['name'];
			$salt = $data['0']['salt'];
			if($Request['password']==NULL)
			{
				$password = $data['0']['password'];
			}
			else{
				$password = md5(md5($Request['password']).$salt);
			}
			
			
			if($image['avatar'] == NULL){
				$avatar = $data['0']['avatar'];
			}
			else{
				$avatar = $image['avatar'];
			}
			$data = array(
					'fullname'				=> $Request['fullname'],
					'email'  				=> $Request['email'],
					'password'				=> $password,
					'address'				=> $Request['address'],
					'phone' 				=> $Request['phone'],
					'gender'				=> $Request['gender'],
					'birthday'				=> $Request['birthday'],
					'id_group'				=> $Request['group'],
					'avatar'				=> $avatar,
			);
			$mode->edit($id,$data);
			$this->view->message = 'Cập Nhật Dữ Liệu Thành Công !!!';	
		}
		$data = $mode->view_where($id);
		$this->view->show_profile = $data;	
	}
	
	
	
	public function lockAction(){
		$mode = new Admin_Model_User;
		$id = $this->getRequest()->getParam('id');
		if($id){
			$data = array(
					'is_delete'	=> 1,
			);
			$mode->lock($id,$data);
			$this->view->message = 'Xóa Dữ Liệu Thành Công !!!';
		}
		$this->_helper->redirector('view');
	}
	
}
